package service.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import db.MyHibernateSessionFactory;
import entity.Users;
import service.UsersDAO;

public class UsersDAOImpl implements UsersDAO {

	@Override
	public boolean usersLogin(Users u) {
		
		Transaction tx = null;
		String hql = "";
		
		try {
			//获得会话对象
			Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
			//开启事务
			tx = session.beginTransaction();
			hql = "from Users where username = ? and password = ?";
			Query query = session.createQuery(hql);
			query.setParameter(0, u.getUsername());
			query.setParameter(1, u.getPassword());
			List<Users> list = query.list();
			tx.commit();
			if (list.size() > 0) {
				return true;
			}else {
				return false;
			}
			
		} catch (Exception ex) {
			
			ex.printStackTrace();
			return false;
		} finally{
			
			if (tx!=null) {
				tx = null;
			}
			
		}
		
	}

}
